<?php include 'header.phtml'; ?>
<style>
<!--
div.entries > div.entry div.entry-content-container div.data {
	max-width: 450px;
}
-->
</style>
<div class="rightbox user-page">
	<div class="user-left-column">
		<?php if(strlen($this->user->getAboutUs()) > 0): ?>
		<div class="user-about-us">
			<h3 class="left"><?php echo $this->translate->_("About us"); ?></h3>
			<?php if($this->admin): ?>
			<a href="<?php echo $this->url(array('controller'=>'accounts', 'action'=>'edit'), 'default', true); ?>" 
					title="<?php echo $this->translate->_('Edit Profile Info'); ?>" class="edit-link left" style="margin-left: 10px; height: 33px;">
				<span></span>
			</a>
			<?php endif; ?>
			<div class="clear"></div>
			<div class="reset"><?php echo $this->user->getAboutUs(); ?></div>
		</div>
		<?php endif; ?>
		<?php if($this->show_map): ?>
		<div class="user-map">
			<h2><?php echo $this->translate->_("Check us on the map"); ?></h2>
			<div id="map_canvas" style="height: 400px;"></div>
		</div>
		<?php elseif(strlen($this->user->getAboutUs()) > 0): ?>
		<div class="clear" style="margin: 0px 0px 56px 0px;"></div>
		<?php else: ?>
		<div class="clear"></div>
		<?php endif; ?>
		<div class="hline">
			<div class="user-dashboard entries">
				<?php echo $this->partial('dashboard/feed.phtml', array(
				        	'translate' => $this->translate,
							'privacy' => $this->privacy,
				        	'results' => $this->results,
							'more' => $this->more,
							'identity' => $this->identity,
							'switch' => "2",
							'hideavatar' => true,
							'user' => $this->user->toArray()
			    		));
				?>
			</div>
			<div class="clear"></div>
		</div>
	</div>
	<div class="right">
		<?php
			if ($this->admin) {
				$all_products_url = $this->url(array('controller'=>'products', 'action'=>'myproducts'), 'default', true);
				$add_product_url = $this->url(array('controller'=>'products', 'action'=>'add'), 'default', true);
			} else {
				$all_products_url = $this->url(array('controller'=>'products', 'action'=>'index', 'owner' => $this->user->getName()), 'default', true);
			}
		?>
		<!-- Products -->
		<?php if (count($this->yourProducts) > 0): ?>
		<div class="green-box">
			<h2>&nbsp;
				<span class="left"><?php echo $this->translate->_("Products"); ?></span>
				<?php if ($this->admin): ?>
				<a href="<?php echo $add_product_url; ?>" class="right edit"><img alt="" src="/images/new/action/product_add.png" /></a>
				<?php endif; ?>
				<?php if(count($this->yourProducts) > 1): ?>
				<a href="<?php echo $all_products_url; ?>" class="right"><?php echo $this->translate->_("see all"); ?></a>
				<?php endif; ?>
			</h2>
			<div class="clear"></div>
			<?php foreach($this->yourProducts as $product):
				// picture control
				$image = "/images/no-pet.jpg";
				if ($product["picture"] && strlen($product["picture"]) > 0) {
					$image = "/images/userfiles/products/{$product["id"]}/small_{$product["picture"]}";
				} ?>
				<a href="<?php echo $this->url(array('controller'=>'products', 'action'=>'view', 'product' => $product["id"]), 'default', true); ?>" 
						class="user-pet" title="<?php echo $product["title"]; ?>">
					<img src="<?php echo $image; ?>" alt="<?php echo $product["title"]; ?>" />
					<div>
						<span><?php echo $product["title"]; ?></span>
						<span><?php echo $product["condition"]; ?></span>
						<span><?php echo $product["pricing"]["primary"]; ?></span>
					</div>
				</a>
			<?php endforeach; ?>
		</div>
		<div class="clear tenpx"></div>
		<?php endif; ?>
		<?php
			if ($this->admin) {
				$all_services_url = $this->url(array('controller'=>'services', 'action'=>'index'), 'default', true);
				$add_service_url = $this->url(array('controller'=>'services', 'action'=>'add'), 'default', true);
			} else {
				$all_services_url = $this->url(array('controller'=>'marketplace', 'action'=>'index', 'owner' => $this->user->getName()), 'default', true);
			}
		?>
		<!-- Services -->
		<?php if (count($this->yourServices) > 0): ?>
		<div class="green-box">
			<h2>&nbsp;
				<span class="left"><?php echo $this->translate->_("Services"); ?></span>
				<?php if ($this->admin): ?>
				<a href="<?php echo $add_service_url; ?>" class="right edit"><img alt="" src="/images/new/action/service_add.png" /></a>
				<?php endif; ?>
				<?php if(count($this->yourServices) > 1): ?>
				<a href="<?php echo $all_services_url; ?>" class="right"><?php echo $this->translate->_("see all"); ?></a>
				<?php endif; ?>
			</h2>
			<div class="clear"></div>
			<?php foreach($this->yourServices as $service):
				// picture control
				$image = "/images/no-pet.jpg";
				if ($service["picture"] && strlen($service["picture"]) > 0) {
					$image = "/images/userfiles/services/{$service["id"]}/small_{$service["picture"]}";
				} ?>
				<a href="<?php echo $this->url(array('controller'=>'services', 'action'=>'view', 'service' => $service["id"]), 'default', true); ?>" 
						class="user-pet" title="<?php echo $service["name"]; ?>">
					<img src="<?php echo $image; ?>" alt="<?php echo $service["name"]; ?>" />
					<div>
						<span><?php echo $service["name"]; ?></span>
						<span><?php echo $service["type"]; ?></span>
					</div>
				</a>
			<?php endforeach; ?>
		</div>
		<div class="clear tenpx"></div>
		<?php endif; ?>
		<?php
			if ($this->admin) {
				$all_pets_url = $this->url(array('controller'=>'pets', 'action'=>'mypets'), 'default', true);
				$add_pet_url = $this->url(array('controller'=>'pets', 'action'=>'add'), 'default', true);
			} else {
				$all_pets_url = $this->url(array('controller'=>'pets', 'action'=>'index', 'owner' => $this->user->getName()), 'default', true);
			}
		?>
		<!-- Pets -->
		<?php if (count($this->yourPets) > 0): ?>
		<div class="green-box">
			<h2>&nbsp;
				<span class="left"><?php echo $this->translate->_("Pets"); ?></span>
				<?php if ($this->admin): ?>
				<a href="<?php echo $add_pet_url; ?>" class="right edit"><img alt="" src="/images/new/action/pet_add.png" /></a>
				<?php endif; ?>
				<?php if(count($this->yourPets) > 1): ?>
				<a href="<?php echo $all_pets_url; ?>" class="right"><?php echo $this->translate->_("see all"); ?></a>
				<?php endif; ?>
			</h2>
			<div class="clear"></div>
			<?php foreach($this->yourPets as $pet): 
				// picture control
				$image = "/images/no-pet.jpg";
				if ($pet["picture"] && strlen($pet["picture"]) > 0) {
					$image = "/images/userfiles/pets/{$pet["id"]}/gallery/small_{$pet["picture"]}";
				} ?>
				<a href="<?php echo $this->url(array('controller'=>'pets', 'action'=>'view', 'pet' => $pet["id"]), 'default', true); ?>" 
						class="user-pet" title="<?php echo $pet["name"]; ?>">
					<img src="<?php echo $image; ?>" alt="<?php echo $pet["name"]; ?>" />
					<div>
						<span><?php echo $pet["name"]; ?></span>
						<span><?php echo $pet["type"]; ?></span>
						<span><?php echo $pet["breed"]; ?></span>
					</div>
				</a>
			<?php endforeach; ?>
		</div>
		<div class="clear tenpx"></div>
		<?php endif; ?>
		<?php
			if ($this->admin) {
				$all_galleries_url = $this->url(array('controller'=>'galleries', 'action'=>'mygalleries'), 'default', true);
				$add_gallery_url = $this->url(array('controller'=>'galleries', 'action'=>'add'), 'default', true);
			} else {
				$all_galleries_url = $this->url(array('controller'=>'galleries', 'action'=>'index', 'owner' => $this->user->getName()), 'default', true);
			}
		?>
		<!-- Galleries -->
		<?php if (count($this->yourGalleries) > 0): ?>
		<div class="green-box">
			<h2>&nbsp;
				<span class="left"><?php echo $this->translate->_("Galleries"); ?></span>
				<?php if ($this->admin): ?>
				<a href="<?php echo $add_gallery_url; ?>" class="right edit"><img alt="" src="/images/new/action/gallery_add.png" /></a>
				<?php endif; ?>
				<?php if(count($this->yourGalleries) > 1): ?>
				<a href="<?php echo $all_galleries_url; ?>" class="right"><?php echo $this->translate->_("see all"); ?></a>
				<?php endif; ?>
			</h2>
			<div class="clear"></div>
			<?php foreach($this->yourGalleries as $gallery):
				// picture control
				$image = "/images/no-pet.jpg";
				if ($gallery["picture"] && strlen($gallery["picture"]) > 0) {
					$image = "/images/userfiles/galleries/{$gallery["id"]}/small_{$gallery["picture"]}";
				} ?>
				<a href="<?php echo $this->url(array('controller'=>'galleries', 'action'=>'view', 'gallery' => $gallery["id"]), 'default', true); ?>" 
						class="user-pet" title="<?php echo $gallery["title"]; ?>">
					<img src="<?php echo $image; ?>" alt="<?php echo $gallery["title"]; ?>" />
					<div>
						<span><?php echo $gallery["title"]; ?></span>
						<span><?php printf(ngettext("%s image", "%s images", $gallery['pictures_count']), $gallery['pictures_count']); ?></span>
					</div>
				</a>
			<?php endforeach; ?>
		</div>
		<?php endif; ?>
	</div>
	<div class="clear"></div>
</div>
<?php $imgsw_path = array(
	'image' => "{custom}",
	'audio' => "{custom}",
	'video' => "http://www.youtube.com/embed/{video}"
); include "../application/modules/frontend/views/scripts/imgsw.phtml"; ?>
<?php $dashboard_location = "dashboard"; include "../application/modules/frontend/views/scripts/dashboard/init.phtml"; ?>
<script type="text/javascript">
	(function($) {
		READY(function() {
			$("a.user-pet").on('mouseenter', function() {
				$(this).find("div").css("display", "none").fadeIn(1000);
			});
	
			$("a.user-pet").on('mouseleave', function() {
				$(this).find("div").css("display", "block").fadeOut(1000);
			});
		
		});
	})(jQuery);
</script>
<?php if($this->show_map): ?>
<script type="text/javascript">
	var MarketsMap = function() {
		var coords = {lat: <?php echo $this->coords[0]; ?>, long: <?php echo $this->coords[1]; ?>},
			map, markers, geocoder, bounds, overlay, current_zoom = -2,
			infoWindow, cached = {}, stop_zoom = false, idle = false;

		function initializeMap() {
			var myOptions = {
				zoom: 1, // this is very important to be 1!!! Chrome doesn't display the map correctly if you add points outsite the viewport
				maxZoom: 15,
				mapTypeId: google.maps.MapTypeId.ROADMAP
			};

			geocoder = new google.maps.Geocoder();
			markers = new Array();
			bounds = new google.maps.LatLngBounds();
			overlay = new google.maps.OverlayView();
			map = new google.maps.Map($('#map_canvas').get(0), myOptions);
			infoWindow = new google.maps.InfoWindow({});
			overlay.setMap(map);
			overlay.draw = function() {};

	    	Petolio.ajax({
				url: 'marketplace/getmarkets',
				type: 'post',
				data: <?php echo json_encode($this->request->getParams()); ?>,
				cache: false,
				success: function (x) {
					if (x.count > 0) {
						clearMarkers();
						placeMarkers(x);
						map.fitBounds(bounds);
					}
				}
			});

			google.maps.event.addListener(map, 'idle', function() {
				idle = true;
			});

			addDragendListener();
			addZoomListener();
		};

		function findAddressNearMe() {
			var rad = $('#search_radius').val(),
				__s = function(position) {
				var lat = position.coords && position.coords.latitude ? position.coords.latitude : position.latitude,
					lng = position.coords && position.coords.longitude ? position.coords.longitude : position.longitude;

				cached = {latitude: lat, longitude: lng};
				map.setCenter(new google.maps.LatLng(lat, lng));
				map.setZoom(13);
				findNearPoint(lng, lat, rad);

				Petolio.hideLoading();
			}, __e = function() {
				Petolio.hideLoading();
				Petolio.showMessage('<?php echo $this->translate->_("Could not determine your location..."); ?>');
			}, __p = function() {
				Petolio.showLoading('<?php echo $this->translate->_("Trying to find your location..."); ?>');
			};

			stop_zoom = true;
			if(!$.isEmptyObject(cached))
				return __s(cached);

			__p();
			$.ajax({
				url : SITE_URL + "marketplace/get-user-address",
				type : 'post',
				cache : false,
				success : function(data) {
					if ( data.latitude && data.longitude && data.latitude.length > 0 && data.longitude.length > 0 ) {
						cached = {latitude: data.latitude, longitude: data.longitude};
						map.setCenter(new google.maps.LatLng(data.latitude, data.longitude));
						map.setZoom(13);
						findNearPoint(data.longitude, data.latitude, rad);

						Petolio.hideLoading();
					} else if ( data.address && data.address.length > 0 ) {
						var input = $('#search_address');
						input.val(data.address);
						input.css({borderColor: '#8F8F8F'});

						geocoder.geocode({'address': input.val()}, function(results, status) {
							if(status == google.maps.GeocoderStatus.OK) {
								var lat = results[0].geometry.location.lat();
								var long = results[0].geometry.location.lng();
								var point = new google.maps.LatLng(lat, long);

								map.setCenter(point);
								map.setZoom(13);
								bounds.extend(point);
								findNearPoint(long, lat, rad);
							} else input.css({borderColor: 'red'});
						});

						Petolio.hideLoading();
					} else {
						if(navigator.geolocation) {
							navigator.geolocation.getCurrentPosition(__s, __e, {
								enableHighAccuracy: true,
								maximumAge: Infinity,
								timeout: 10000
							});
						}
					}
				}
			});
		};

		function addZoomListener() {
			google.maps.event.addListener(map, 'zoom_changed', function() {
				if (map.getZoom() != current_zoom && stop_zoom == false && idle == true) {
					idle = false;
					current_zoom = map.getZoom();

					var nw = overlay.getProjection().fromContainerPixelToLatLng(new google.maps.Point(0, 0));
					var se = overlay.getProjection().fromContainerPixelToLatLng(new google.maps.Point($("#map_canvas").width(), $("#map_canvas").height()));

					var params = {
							longitude_from : nw.lng(),
							latitude_from : se.lat(),
							longitude_to : se.lng(),
							latitude_to : nw.lat()
						};

					Petolio.ajax({
						url: 'marketplace/getmarkets',
						type: 'post',
						data: $.extend({}, params, <?php echo json_encode($this->request->getParams()); ?>),
						cache: false,
						success: function (x) {
							if (x.count > 0) {
								clearMarkers();
								placeMarkers(x);
							}
						}
					});
				}
			});
		};

		function addDragendListener() {
			google.maps.event.addListener(map, 'dragend', function() {
				if(idle == true) {
					idle = false;

					var nw = overlay.getProjection().fromContainerPixelToLatLng(new google.maps.Point(0, 0));
					var se = overlay.getProjection().fromContainerPixelToLatLng(new google.maps.Point($("#map_canvas").width(), $("#map_canvas").height()));

					var params = {
							longitude_from : nw.lng(),
							latitude_from : se.lat(),
							longitude_to : se.lng(),
							latitude_to : nw.lat()
						};
					
			    	Petolio.ajax({
						url: 'marketplace/getmarkets',
						type: 'post',
						data: $.extend({}, params, <?php echo json_encode($this->request->getParams()); ?>),
						cache: false,
						success: function (x) {
							if (x.count > 0) {
								clearMarkers();
								placeMarkers(x);
							}
						}
					});
				}
			});
		};

		function findAddress(e) {
			e.preventDefault();

			var input = $('#search_address'),
				rad = $('#search_radius').val();

			input.css({borderColor: '#8F8F8F'});

			geocoder.geocode({'address': input.val()}, function(results, status) {
				if(status == google.maps.GeocoderStatus.OK) {
					var lat = results[0].geometry.location.lat();
					var long = results[0].geometry.location.lng();
					var point = new google.maps.LatLng(lat, long);

					map.setCenter(point);
					map.setZoom(13);
					bounds.extend(point);
					findNearPoint(long, lat, rad);
				} else input.css({borderColor: 'red'});
			});
		};

		function findNearPoint(lng, lat, rad) {
			Petolio.ajax({
				url: 'marketplace/getmarkets',
				type: 'post',
				data: {
					longitude : lng,
					latitude : lat,
					radius : rad
				},
				cache: false,
				success: function (x) {
					if (x.count > 0) {
						clearMarkers();
						placeMarkers(x);
						map.fitBounds(bounds);
					}
					stop_zoom = false;
				}
			});
		};

		function clearMarkers() {
			for ( var i = 0; i < markers.length; i++) {
				markers[i].setMap(null);
			}

			markers = new Array();
			bounds = new google.maps.LatLngBounds();
		};

		function placeMarkers(data) {
			jQuery.each(data.items, function(i, item) {
				var point = new google.maps.LatLng(parseFloat(item.latitude), parseFloat(item.longitude));
				bounds.extend(point);

				var marker = new google.maps.Marker({
					position : point,
					map : map
				});
				marker.setTitle(item.name);

				google.maps.event.addListener(marker, 'click', function() {
					if (infoWindow)
						infoWindow.close();

					// Content for the infoWindow
					var html = "<div style='height: 85px'><span style='font-size: 12px'><b>" + item.name + "</b> - " + item.type
						+ "</span><br /><a href='/accounts/view/user/" + item.userid + "'>" + item.username + "</a><br />" + item.address + "<hr />"
						+ "<a href='/services/view/service/" + item.id + "'>" + item.view + "</a></div>";

					infoWindow.setContent(html);
					infoWindow.open(map, marker);
				});

				markers[markers.length] = marker;
				var point = new google.maps.LatLng(parseFloat(item.latitude), parseFloat(item.longitude));

				bounds.extend(point);
			});
		};

		function __construct(s) {
			store = s;

			$('body').append('<script type="text/javascript" src="'+ window.location.protocol +'//maps.googleapis.com/maps/api/js?v=3.7&sensor=false&callback=MarketsMap.go" />');
		};

		function fitb() {
			map.fitBounds(bounds);
		};

		return {
			init: __construct,
			go: initializeMap,
			search: findAddress,
			nearme: findAddressNearMe,
			placeMarkers: placeMarkers,
			fitb: fitb
		};
	}();

	READY(function() {
		MarketsMap.init();
	});
</script>
<?php endif; ?>